<?php
namespace GuessSong\Controller;
use Common\Controller\GuessSongController;
class LoginController extends GuessSongController {



    /**
     * 小程序登录
     * @param string
     * @return string
     */
    public function wxlogin(){

        $code =  I('post.code','','htmlspecialchars');
        $appid =  I('post.appid','','htmlspecialchars');
        $secret =  I('post.secret','','htmlspecialchars');

        $nickName = I('post.nickName','','htmlspecialchars');
        $avatarUrl = I('post.avatarUrl','','htmlspecialchars');
        $gender = I('post.gender','','htmlspecialchars');
        $province = I('post.province','','htmlspecialchars');
        $city = I('post.city','','htmlspecialchars');
        $country = I('post.country','','htmlspecialchars');
        $language = I('post.language','','htmlspecialchars');


        if(!$code||!$appid||!$secret){
            $data = array();

            $this->returnData($data,$msg='参数错误',$code=401);

        }

        $url = "https://api.weixin.qq.com/sns/jscode2session?appid=$appid&secret=$secret&js_code=$code&grant_type=authorization_code";

        $res = $this->curlget($url);

        if (!$res['openid']) {
            $this->returnData($res,$msg='openid获取失败',$code=100);
        }


        if ($res['openid']){

            $where= array(
                'openId'=>$res['openid']
            );

            $user = M('SongMember')->field('song_member_id,nickName,gender,city,province,country,avatarUrl,sort,openId')->where($where)->find(); //判断是否有用户信息

            if ($user){
                $user_id = $user['song_member_id'];
                $userinfo = array(
                    'song_member_id'=>$user['song_member_id'],
                    'nickName'=>$nickName,
                    'gender'=>$gender,
                    'city'=>$city,
                    'province'=>$province,
                    'country'=>$country,
                    'avatarUrl'=>$avatarUrl,
                    'language'=>$language,
                    'uinionid' =>$res['unionId']||'',
                    'logintime' =>time(),
                    'sort'=>$user['sort'],
                    'openId' =>$res['openid'],
                    'appid'=>$appid,
                    'session_key'=>$res['session_key'],
                );

                M('SongMember')->where($where)->save($userinfo);

            }else{

                $user_id= M('SongMember')->max('song_member_id')+1;
                $userinfo = array(
                    'song_member_id'=>$user_id,
                    'nickName'=>$nickName,
                    'gender'=>$gender,
                    'city'=>$city,
                    'province'=>$province,
                    'country'=>$country,
                    'avatarUrl'=>$avatarUrl,
                    'language'=>$language,
                    'uinionid' =>$res['unionId']||'',
                    'logintime' =>time(),
                    'openId' =>$res['openid'],
                    'createtime'=>time(),
                    'appid'=>$appid,
                    'sort'=>$user_id,
                    'prize'=>'0',
                    'session_key'=>$res['session_key'],
                    'opportunity'=>2,
                );

                M('SongMember')->data($userinfo)->add();

            }

        }

        $session_id =   $this->getSessionId();
        //保存数据到session
        //session('userinfo', $userinfo);

        $data = array(
            'openid'=>$res['openid'],
            'session_id' =>$session_id,
            'member_id' =>$user_id
        );

        $this->returnData($data,'成功',200);


    }





}
